//
//  HomeTopCell.m
//  Cat
//
//  Created by lushitong on 2018/12/17.
//  Copyright © 2018年 lushitong. All rights reserved.
//

#import "HomeTopCell.h"
#import "TYCyclePagerView.h"
#import "TYPageControl.h"
#import "HomeBannerCell.h"
#import "HomePriorityInfo.h"
#import "WebViewController.h"

@interface HomeTopCell()<TYCyclePagerViewDataSource, TYCyclePagerViewDelegate>

@property (nonatomic, strong) TYCyclePagerView *pagerView;
@property (nonatomic, strong) TYPageControl *pageControl;

@end

@implementation HomeTopCell

- (void)setBannerArray:(NSArray *)bannerArray {
	_bannerArray = bannerArray;
	self.pageControl.numberOfPages = bannerArray.count;
	[self.pagerView reloadData];
}
- (void)setDelegate:(id<HomeTopBannerDelegate>)delegate {
	_delegate = delegate;
}

- (instancetype)initWithFrame:(CGRect)frame {
	self = [super initWithFrame:frame];
	if (self) {
		self.backgroundColor = COLOR_WITH_HEX(0xFAFAFA);
		[self loadSubviews];
	}
	return self;
}

- (void)loadSubviews {
	[self.contentView addSubview:self.pagerView];
	[self.contentView addSubview:self.pageControl];
	
	[self.pagerView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.contentView.mas_left);
		make.width.equalTo(@(ScreenWidth));
		make.top.equalTo(self.contentView.mas_top);
		make.height.equalTo(@([HomeBannerCell getHomeBannerSize]));
	}];
	
	[self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.pagerView.mas_left);
		make.right.equalTo(self.pagerView.mas_right);
		make.height.equalTo(@(kQKFitModuleDecimal(4.0)));
		make.bottom.equalTo(self.contentView.mas_bottom);
		make.width.lessThanOrEqualTo(@(kQKFitModuleDecimal(100.0)));
	}];
}

#pragma mark----- 代理方法 -----

- (NSInteger)numberOfItemsInPagerView:(TYCyclePagerView *)pageView {
	return self.bannerArray.count;
}

- (UICollectionViewCell *)pagerView:(TYCyclePagerView *)pagerView cellForItemAtIndex:(NSInteger)index {
	HomeBannerCell *cell = [pagerView dequeueReusableCellWithReuseIdentifier:@"HomeBannerCellID" forIndex:index];
	BannersModel *model = [self.bannerArray safeObjectAtIndex:index];
	[cell setModel:model];
	return cell;
}

- (TYCyclePagerViewLayout *)layoutForPagerView:(TYCyclePagerView *)pageView {
	TYCyclePagerViewLayout *layout = [[TYCyclePagerViewLayout alloc]init];
	layout.itemSize = [HomeBannerCell getHomeBannerSize];
	layout.itemSpacing = kQKFitModuleDecimal(10);
	layout.itemHorizontalCenter = YES;
	layout.layoutType = TYCyclePagerTransformLayoutLinear;
	return layout;
}

- (void)pagerView:(TYCyclePagerView *)pageView didScrollFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
	self.pageControl.currentPage = toIndex;
}

- (void)pagerView:(TYCyclePagerView *)pageView didSelectedItemCell:(__kindof UICollectionViewCell *)cell atIndex:(NSInteger)index {
	BannersModel *model = [self.bannerArray safeObjectAtIndex:index];
	WebViewController *webVc = [[WebViewController alloc] init];
	webVc.URL = model.targetURL;
	[self.currentNavigationController pushViewController:webVc animated:YES];
}

#pragma mark----- lazy init -----

- (TYCyclePagerView *)pagerView {
	if (!_pagerView) {
		_pagerView = [[TYCyclePagerView alloc] init];
		_pagerView.dataSource = self;
		_pagerView.delegate = self;
		_pagerView.autoScrollInterval = 3.0;
		_pagerView.isInfiniteLoop = YES;
		[_pagerView registerClass:[HomeBannerCell class] forCellWithReuseIdentifier:@"HomeBannerCellID"];
	}
	return _pagerView;
}

- (TYPageControl *)pageControl {
	if (!_pageControl) {
		_pageControl = [[TYPageControl alloc] init];
		_pageControl.currentPageIndicatorSize = CGSizeMake(kQKFitModuleDecimal(19), kQKFitModuleDecimal(2));
		_pageControl.pageIndicatorSize = CGSizeMake(kQKFitModuleDecimal(9), kQKFitModuleDecimal(2));
		_pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
		_pageControl.pageIndicatorTintColor = COLOR_WITH_HEX(0xB5B5B5);
		_pagerView.layer.masksToBounds = YES;
	}
	return _pageControl;
}

@end
